import { reqGoodsInfo } from "../../http/api";
//初始值
const initState = {
  info: {},
};

//action types
const types = {
  CHANGE_DETAIL_INFO: "CHANGE_DETAIL_INFO",
};

//action creator
export const actions = {
  //改变info
  changeinfo: (info) => ({
    type: types.CHANGE_DETAIL_INFO,
    info,
  }),
  reqInfo: (id, type) => {
    return (dispatch, getState) => {
      reqGoodsInfo({ id, type }).then((res) => {
        if (res.data.code === 200) {
          dispatch(actions.changeinfo(res.data.list ? res.data.list[0] : {}));
        }
      });
    };
  },
};

//reducer
export const detailReducer = (state = initState, action) => {
  switch (action.type) {
    case types.CHANGE_DETAIL_INFO:
      return {
        ...state,
        info: action.info,
      };
    default:
      return state;
  }
};

//导出数据
export const getdetail = (state) => state.detail.info;
